package control;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import main.Res;

public class TextButton extends Control {
	protected String text;
	protected boolean enable = true;
	protected boolean isHover = false;
	protected boolean isTogled = false;

	int x, y;
	int w, h;
	protected int color;

	public TextButton(String text, int x, int y, int w, int h) {

		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		this.text = text;
		color = 0xffffffff;
	}

	public boolean isTogled() {
		return isTogled;
	}

	public void setTogled(boolean isTogled) {
		this.isTogled = isTogled;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	protected boolean isHover(int x, int y) {
		return (x > this.x && y > this.y && x < this.x + this.w && y < this.y
				+ this.h);
	}

	public boolean pointerPressed(int x, int y) {
		if (!enable) {
			return false;
		}
		if (isHover(x, y)) {
			isHover = true;
			return true;
		}
		isHover = false;
		return false;
	}

	public boolean pointerReleased(int x, int y) {
		if (!enable) {
			return false;
		}
		isHover = false;
		if (isHover(x, y)) {
			if (command != null) {
				command.Do();
			}
			return true;
		}
		return false;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	protected void controlPaint(Graphics g) {
		if (isTogled) {
			g.setColor(color);
			// g.fillRect(x, y, sprite.getWidth(), sprite.getHeight());
			g.fillRoundRect(x, y, w, h, 10, 10);
		}
		g.setColor(color);
		if (isHover) {
			g.setFont(fontB);
			g.drawString(text, x+2, y+2, 0);
		}else{
			g.setFont(fontU);
			g.drawString(text, x, y, 0);
		}
	}
}
